<?php
/**
 * 语言类
 */


class Lang extends MyBase{
	
	function __construct() {
		parent::__construct('lang.id', array('name' => 'name', 'mark' => 'mark'));
	}
	
	/**
	 * 修改语言
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('mark' => $post['mark']);
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)) {return lang('already').lang('exist');}
		$data = array(
			'name'		=> $post['name'],
			'mark'		=> $post['mark'],
			'memo'		=> $post['memo'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_lang(); // 缓存语言
		}
		return $r;
	}
			
	/**
	 * 删除语言
	 *
	 * @param int			$id
	 * @return int
	 */
	public function del($post){
		$iRtn = 0;
		if(!is_array($post['list'])){
			$post['list'] = array($post['list']);
		}
		$oLibrary = new Library();
		foreach($post['list'] as $lang_id){
			$librarys = $oLibrary->get_list('id', array('pid' => $lang_id), array(), 1, 1);
			if(!empty($librarys['list'])){
				continue;
			}
			if($this->db->delete($this->tbname, array('id' => $lang_id))){
				$iRtn++;
			}
		}
		return $iRtn;
	}
		
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['name'])){
			$select['kept'] .= " AND `name` LIKE '%{$select['name']}%'";
			unset($select['name']);
		}
		if(!empty($select['mark'])){
			$select['kept'] .= " AND `mark` LIKE '%{$select['mark']}%'";
			unset($select['mark']);
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	public function get_all($fields = '*', $select = array(), $order = array()){
		$select['kept'] = '';
		if(!empty($select['name'])){
			$select['kept'] .= " AND `name` LIKE '%{$select['name']}%'";
			unset($select['name']);
		}
		if(!empty($select['mark'])){
			$select['kept'] .= " AND `mark` LIKE '%{$select['mark']}%'";
			unset($select['mark']);
		}
		return $this->db->get_all($this->tbname, $fields, $select, $order);
	}
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
		
	public function cache_lang(){
		$list = $this->get_all();
		if(!empty($list['list'])){
			$lang_list = array();
			$lang_mark_list = array();
			$lang_id_name_list = array();
			foreach($list['list'] as $lang){
				$lang_list[$lang['id']] = $lang;
				$lang_mark_list[$lang['mark']] = $lang;
				$lang_id_name_list[$lang['id']] = $lang['name'];
			}
			return cache_write('langs.php', array('lang_list' => $lang_list, 'lang_mark_list' => $lang_mark_list, 'lang_id_name_list' => $lang_id_name_list));
		}
		return -1;
	}
	
}
?>